#include "Book_t.h" 

Book_t::Book_t(Name name,Author author,ISBN isbn, unsigned int num_copies) : name(name), author(author), isbn(isbn), num_copies(num_copies) 
{
	list_of_borrowers = new list<Borrower_t*>(); 
	list_of_waiting = new list<Borrower_t*>();
}
Book_t::~Book_t()
{
	delete list_of_borrowers;
	delete list_of_waiting;
}
bool Book_t::isAvailable() const
{
	return (getNofAvailableCopies() > 0);
}

unsigned int Book_t::getNofAvailableCopies() const
{
	return num_copies - list_of_borrowers->size();
}
unsigned int Book_t::getNofCopies() const
{
	return num_copies;
}
bool Book_t::setNofCopies(unsigned int noc)
{
	num_copies = noc;
	return true;
}
ISBN Book_t::getISBN() const
{
	return isbn;
}

const Name& Book_t::getName() const
{
	return name;
}
const Author& Book_t::getAuthor() const
{
	return author;
}

bool Book_t::borrowBook(Borrower_t* borrower_p)
{
	if(!isAvailable())
	{
			list_of_waiting->push_front(borrower_p);
			return false;
	}
	list_of_borrowers->push_front(borrower_p); 
	return true;
}
Borrower_t* Book_t::returnBook(Borrower_t* borrower_p)
{ 
	list_of_borrowers->remove(borrower_p);
	if (list_of_waiting->size() > 0 && isAvailable())
	{	
		Borrower_t* borrower_tmp = list_of_waiting->back();
		list_of_borrowers->push_front(borrower_tmp);
		list_of_waiting->pop_back();
		return borrower_tmp;
	}
	return 0;
}